<?php declare(strict_types=1);
/**
 * @author      guosir
 * @copyright   2023-2025 owner
 **/

namespace Srv\Apps\Apis\Wallet;

use Srv\Apps\Apis\Common\DefCtl\DefBillsBizCtl;
use Srv\Apps\Apis\Log\Ctl\LogCtl;
use Srv\Apps\Apis\Wallet\Ctl\WalletCtl;
use Srv\Apps\Proto\DTO\Wallet\WalletBalanceListDTO;
use Srv\Apps\Proto\DTO\Wallet\WalletBalanceRechargeCoinDTO;
use Srv\Apps\Proto\DTO\Wallet\WalletBalanceRecordListDTO;
use Srv\Apps\Proto\Enum\ErrCodeEnum;
use Srv\Apps\Proto\Enum\LangKeyEnum;
use Srv\Apps\Proto\Enum\Log\LogResultEnum;
use Srv\Apps\Proto\VO\Wallet\WalletBalanceListVO;
use Srv\Apps\Proto\VO\Wallet\WalletBalanceRecordListVO;
use Srv\Libs\ConfModel\RouteConfData;
use Srv\Libs\Frame\ActionAbstract;
use Srv\Libs\Frame\AuthData;
use Srv\Libs\Frame\DTOAbstract;
use Srv\Libs\Frame\Lang;
use Srv\Libs\Frame\Page;
use Srv\Libs\Frame\ResponseData;
use Srv\Libs\Frame\VOAbstract;

final class WalletAct extends ActionAbstract
{
    /**
     * @url /wallet/balance/list
     * @auth walletBalanceList
     * @req Srv.Apps.Proto.DTO.Wallet.WalletBalanceListDTO
     * @rsp Srv.Apps.Proto.VO.Wallet.WalletBalanceListVO
     * 钱包账户列表
     */
    public function getWalletBalanceList(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        if(!($DTOClass instanceof WalletBalanceListDTO) || !($VOClass instanceof WalletBalanceListVO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $Page                   = new Page();
        $currPage               = max(1, $DTOClass->getCurrPage());
        $pageSize               = $DTOClass->getPageSize() < 1 ? 12 : $DTOClass->getPageSize();
        $Page->init(0, $pageSize, $currPage);
        $whereData              = $RouteConfData->getParamsResData($DTOClass);
        $VOClass                = WalletCtl::tag()->getWalletBalanceList($Page, $whereData);
        return ResponseData::rspSuccAuto('', $VOClass, []);
    }

    /**
     * @url /wallet/balance/record/list
     * @auth walletBalanceRecordList
     * @req Srv.Apps.Proto.DTO.Wallet.WalletBalanceRecordListDTO
     * @rsp Srv.Apps.Proto.VO.Wallet.WalletBalanceRecordListVO
     * 钱包账户记录列表
     */
    public function getWalletBalanceRecordList(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        if(!($DTOClass instanceof WalletBalanceRecordListDTO) || !($VOClass instanceof WalletBalanceRecordListVO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $Page                   = new Page();
        $currPage               = max(1, $DTOClass->getCurrPage());
        $pageSize               = $DTOClass->getPageSize() < 1 ? 12 : $DTOClass->getPageSize();
        $Page->init(0, $pageSize, $currPage);
        $whereData              = $RouteConfData->getParamsResData($DTOClass);
        $VOClass                = WalletCtl::tag()->getWalletBalanceRecordList($Page, $whereData);
        return ResponseData::rspSuccAuto('', $VOClass, []);
    }

    /**
     * @url /wallet/balance/recharge/coin
     * @auth walletBalanceRechargeCoin
     * @req Srv.Apps.Proto.DTO.Wallet.WalletBalanceRechargeCoinDTO
     * 充值Coin
     */
    public function walletBalanceRechargeCoin(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        unset($RouteConfData, $VOClass);
        if(!($DTOClass instanceof WalletBalanceRechargeCoinDTO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $admUserId      = AuthData::getLoginUserId();
        $userId         = intval($DTOClass->getUserId());
        $valueOrder     = $DTOClass->getValue();
        $dbbzId         = DefBillsBizCtl::tag()->getIdByKey(DefBillsBizCtl::KEY_RECHARGE_ADMIN);
        $balanceData    = ['admUserId' => $admUserId];
        $bizData        = $balanceData;
        $logData        = ['userId' => $userId, 'dbbzId' => $dbbzId, 'admUserId' => $admUserId, 'value' => $valueOrder];
        $LogCtl         = LogCtl::tag();
        $result         = WalletCtl::tag()->incWalletBalanceForCoin($userId, $valueOrder, $dbbzId, $bizData, $balanceData, false);
        if(!$result) {
            $msgText    = '充值Coin失败';
            $LogCtl->logAdd(LogCtl::WALLET_COIN_RECHARGE, LogResultEnum::LOG_RESULT_FAIL, $msgText, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $msgText        = '充值Coin成功';
        $LogCtl->logAdd(LogCtl::WALLET_COIN_RECHARGE, LogResultEnum::LOG_RESULT_SUCCESS, $msgText, $logData);
        return ResponseData::rspSuccAuto('', null, []);
    }
}